package com.share.common.common;

import com.share.common.enums.ErrMsgEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回数据结果集
 *
 * @author Administrator
 * @data 2019/6/9
 */
@Data
public final class Result<T> implements Serializable {
    private String msg;
    private T data;
    private String code;

    public static <T> Result<T> success() {
        return new Result<T>(ErrMsgEnum.SUCCESS.getCode(), null, ErrMsgEnum.SUCCESS.getName());
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result<T>(ErrMsgEnum.SUCCESS.getCode(), data, msg);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(ErrMsgEnum.SUCCESS.getCode(), data, null);
    }

    public static <T> Result<T> failure(String msg, T data) {
        return new Result<T>(ErrMsgEnum.FAILURE.getCode(), data, msg);
    }

    public static Result failure(String msg) {
        return new Result(ErrMsgEnum.FAILURE.getCode(), null, msg);
    }

    public Result(String code, T data, String msg) {
        this.msg = msg;
        this.data = data;
        this.code = code;
    }

    public Result() {
    }

    /**
     * 仅在controller操作返回值是insert  update  delete时使用
     *
     * @auther sunyanqiang
     * @date 2019/6/17 15:46
     */
    public static <T> Result<T> insertOrDeleteOrUpdate(int reCode) {
        return insert(reCode, null);
    }

    /* 插入成功通过 obj返回主键*/
    public static <T> Result<T> insert(int reCode, T obj) {
        if (reCode > 0) {
            return success(obj);
        } else {
            return failure(ErrMsgEnum.FAILURE);
        }
    }

    public static <T> Result<T> bool(Boolean bool, String errMsg) {
        if (bool == null || bool) {
            Result<T> failure = failure(ErrMsgEnum.FAILURE);
            failure.setMsg(errMsg);
            return failure;
        } else {
            return failure(ErrMsgEnum.SUCCESS);
        }
    }

    public static <T> Result<T> bool(Boolean bool) {
        if (bool == null || !bool) {
            return failure(ErrMsgEnum.FAILURE);
        } else {
            return failure(ErrMsgEnum.SUCCESS);
        }
    }

    public static <T> Result<T> failure(ErrMsgEnum enu) {
        Result result = new Result();
        result.code = enu.getCode();
        result.msg = enu.getName();
        return result;
    }

    public static <T> Result<T> failure(ErrMsgEnum enu, String msg) {
        Result result = new Result();
        result.code = enu.getCode();
        result.msg = enu.getName();
        result.data = msg;
        ;
        return result;
    }

    public static <T> Result<T> failure(ErrMsgEnum enu, T data) {
        Result result = new Result();
        result.code = enu.getCode();
        result.data = data;
        return result;
    }

    public static <T> Result<T> failure(T data) {
        Result result = new Result();
        result.code = ErrMsgEnum.FAILURE.getCode();
        result.data = data;
        return result;
    }
}
